import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { CrudConfigService } from '@nestjsx/crud';
import { utilities as nestWinstonModuleUtilities, WinstonModule } from 'nest-winston';
import * as winston from 'winston';
import { AppModule } from './app.module';

CrudConfigService.load({
  query: {
    cache: 1000 * 60, 
  },
  params: {
    id: {
      field: 'id',
      type: 'number',
      primary: true,
    },
  },
  routes: {
    only: ['getOneBase', 'getManyBase'],
    exclude: ['createManyBase', 'createOneBase', 'deleteOneBase', 'replaceOneBase', 'updateOneBase'],
    createOneBase : {
      returnShallow : true
    },
    createManyBase : {
    },
  },
});



async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: WinstonModule.createLogger({
      transports: [
        new winston.transports.Console({
          format: winston.format.combine(
            winston.format.timestamp(),
            nestWinstonModuleUtilities.format.nestLike(),
          ),
          level: 'error'
        }),
        // other transports...
      ],
    })
  })

  app.setGlobalPrefix('web-api');
  app.useGlobalPipes(new ValidationPipe());
  app.enableCors()

  const options = new DocumentBuilder()
    .setTitle('手机端的接口文档')
    .setDescription('手机端的接口文档说明')
    .setVersion('1.0')
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('/web-api', app, document);


  await app.listen(process.env.WEB_PORT, () => {
    // userInit()
    // villagerInit()
    // areaInit()
  });
}



bootstrap();

